package com.example.controller;

import com.example.Service.FinanceRecordService;
import com.example.entity.FinanceRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/finance")
@CrossOrigin(origins = "*")
public class FinanceRecordController {

    @Autowired
    private FinanceRecordService financeRecordService;

    @PostMapping("/add")
    public String addRecord(@RequestBody FinanceRecord record) {
        return financeRecordService.addFinanceRecord(record) > 0 ? "success" : "fail";
    }

    @DeleteMapping("/delete/{recordId}")
    public String deleteRecord(@PathVariable Integer recordId) {
        return financeRecordService.deleteFinanceRecordById(recordId) > 0 ? "success" : "fail";
    }

    @PutMapping("/update")
    public String updateRecord(@RequestBody FinanceRecord record) {
        return financeRecordService.updateFinanceRecord(record) > 0 ? "success" : "fail";
    }

    @GetMapping("/user/{userId}")
    public List<FinanceRecord> getRecordsByUserId(@PathVariable Integer userId) {
        return financeRecordService.getFinanceRecordsByUserId(userId);
    }
}
